// pages/coupon-list/index.js
const { getCouponList } = require('../../api/market');
const { MEMBER_COUPON_TABSLIST } = require('../../constants/member');
const { PageBase } = require('../../mixins/page');

Page({
    mixins: [PageBase],
    data: {
        activeTab: 0,
        tabList: MEMBER_COUPON_TABSLIST,
        couponList: [],
        showDetail: false,
        couponInfo: null,
    },
    async onLoad(options) {
        await this._getCoupons();
    },
    async _getCoupons() {
        const { tabList, activeTab } = this.data;
        const item = tabList[activeTab];
        const couponList = await getCouponList(item.type);
        console.log('CouponList => 获取到的优惠券列表为：', couponList);
        this.setData({
            couponList,
        });
    },
    changeTabAction(event) {
        const { index } = event.detail;
        this.setData({
            activeTab: index,
        }, async () => await this._getCoupons());
    },
    openCouponDetail(event) {
        const info = event.detail;
        console.log('优惠券的详情为：', info);
        this.setData({
            showDetail: true,
            couponInfo: info,
        });
    },
    closeDetail() {
        this.setData({
            showDetail: false,
        });
    },
})